package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.service.Impl.UserServiceImpl;
import com.qf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/active")
public class ActiveServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        String code = request.getParameter("code");

        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(code)){
            response.getWriter().write("没有参数");
            return;
        }
        try {
            int active = userService.active(username, code);
            if (active == 1){
                response.getWriter().write("激活成功");
            }else {
                response.getWriter().write("激活失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("/error/error.jsp");
        }
    }
}
